<?php
/**
* Storage Management package.
* This class implements the Storage interface for the Client Side. This interface is made up of the
* following actions:
* <ul><li><b>draw</b>: Draw the template for the management panel</li>
* <li><b>delete</b>: Delete a file or folder</li>
* <li><b>create_folder</b>: Create a new folder</li>
* <li><b>upload</b>: Upload a file</li>
* <li><b>get</b>: Get details on a file/folder</li>
*</ul>
*@package Management
*@subpackage Storage
*@author Willy Gardiol <willy@gardiol.org>
*@version 1.0
*/
class MgmStorage extends MgmRequest
{
	protected $actions = array( "draw" => "DrawTemplate",
								"delete" => "DeleteItem",
								"create_folder" => "CreateFolder",
								"upload" => "UploadFile",
								"get" => "GetItemData" );

	protected function DeleteItem()
	{
		$path = Core::PostVar("path", "", "string");
		if ( CoreAuth::Authorize() )
			$this->SetOutput("result", Storage::Delete( $path ) );
			
	}

	protected function UploadFile()
	{
		$path = Core::PostVar("path", "", "string");
		 if ( CoreAuth::Authorize() )
			$this->SetOutput("result", Storage::Upload( $path, "storage_upload" ) );
	}

	protected function CreateFolder()
	{
		$path = Core::PostVar("path", "", "string");
		$new_path = Core::PostVar("new_path", false, "string");
		if ( CoreAuth::Authorize() && ($new_path !== false)  )
			$this->SetOutput("result", Storage::CreateFolder( "$path/$new_path" ) );
	}

	protected function DrawTemplate()
	{
		$template = new HtmlTemplate("core:management_storage",
							array(
								"path" => Core::PostVar("path", "", "string")
								));
		$template->DrawHtml();
	}

	protected function GetItemData()
	{
		$path = Core::PostVar("path", "", "string");
		$item = Storage::Get($path);
		if ( $item !== false )
		{
			$data = $item->GetDetails();
			if ( $data["type"] == "folder" )
			{
				$data["leaves"] = array();
				foreach ($item->GetLeaves() as $k => $leaf )
				{
					$tmp =  $leaf->GetDetails();
					$data["leaves"][$k] = array("type" => $tmp["type"],
												"name" => $tmp["name"]);
				}
			}
			else
				$ret["leaves"] = false;
			$this->SetOutput( "item", $data );
		}
		else
			$this->SetError(I18N("This path '$path' is invalid"));
	}
}
?>
